package com.zyf.project.kvdb.protocol.handler;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.socket.SocketChannel;

/**
 * @author Zhou Yifan
 */
public class KVDBInitializerHandler extends ChannelInitializer<SocketChannel> {

    private final KVDBHandler kvdbHandler;

    public KVDBInitializerHandler(KVDBHandler kvdbHandler) {
        this.kvdbHandler = kvdbHandler;
    }

    /**
     * <h1>初始化 channel </h1>
     * @param channel  {@link SocketChannel}
     */
    @Override
    protected void initChannel(SocketChannel channel) {
        kvdbHandler.channel(channel);
    }

    /**
     * <h1>关闭 channel 连接</h1>
     * @param channelHandlerContext {@link ChannelHandlerContext}
     */
    @Override
    public void channelInactive(ChannelHandlerContext channelHandlerContext) {
        kvdbHandler.disconnected(channelHandlerContext);
    }
}
